package View.Dorm;

import Service.DormService;
import Service.ServiceImpl.DormServiceImpl;
import View.Index.Index_admin;
import domain.Dorm;
import domain.User;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AddDorm extends JFrame {

    public AddDorm(User user){
        init();
        componentInit(user);
    }

    public void init(){
        this.setTitle("添加宿舍");
        this.setSize(616,455);
        this.setResizable(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(null);
    }
    public void componentInit(User user){
        //------------------JLabel--------------------
        JLabel label1= new JLabel();
        label1.setText("宿舍号:");
        label1.setBounds(64,20,65,30);
        JLabel label2= new JLabel();
        label2.setText("成员1:");
        label2.setBounds(332,19,65,30);
        JLabel label3= new JLabel();
        label3.setText("成员2:");
        label3.setBounds(64,79,65,30);
        JLabel label4= new JLabel();
        label4.setText("成员3:");
        label4.setBounds(333,80,65,30);
        JLabel label5= new JLabel();
        label5.setText("成员4:");
        label5.setBounds(64,135,65,30);
        JLabel label6= new JLabel();
        label6.setText("成员5:");
        label6.setBounds(333,137,65,30);
        JLabel label7= new JLabel();
        label7.setText("成员6:");
        label7.setBounds(64,184,65,30);
        JLabel label8= new JLabel();
        label8.setText("宿舍长:");
        label8.setBounds(334,193,65,30);

        //------------------JTextField--------------------
        JTextField field1= new JTextField();
        field1.setText("");
        field1.setBorder(new LineBorder(Color.lightGray));
        field1.setBounds(140,20,120,30);
        JTextField field2= new JTextField();
        field2.setText("");
        field2.setBorder(new LineBorder(Color.lightGray));
        field2.setBounds(402,18,120,30);
        JTextField field3= new JTextField();
        field3.setText("");
        field3.setBorder(new LineBorder(Color.lightGray));
        field3.setBounds(139,80,120,30);
        JTextField field4= new JTextField();
        field4.setText("");
        field4.setBorder(new LineBorder(Color.lightGray));
        field4.setBounds(404,80,120,30);
        JTextField field5= new JTextField();
        field5.setText("");
        field5.setBorder(new LineBorder(Color.lightGray));
        field5.setBounds(139,134,120,30);
        JTextField field6= new JTextField();
        field6.setText("");
        field6.setBorder(new LineBorder(Color.lightGray));
        field6.setBounds(408,139,120,30);
        JTextField field7= new JTextField();
        field7.setText("");
        field7.setBorder(new LineBorder(Color.lightGray));
        field7.setBounds(139,182,120,30);
        JTextField field8= new JTextField();
        field8.setText("");
        field8.setBorder(new LineBorder(Color.lightGray));
        field8.setBounds(410,193,120,30);

        //------------------JButton--------------------
        JButton button1= new JButton();
        button1.setText("添加");
        button1.setBounds(162,274,95,27);
        JButton button2= new JButton();
        button2.setText("取消");
        button2.setBounds(358,277,95,27);





        this.add(label1);
        this.add(label2);
        this.add(label3);
        this.add(label4);
        this.add(label5);
        this.add(label6);
        this.add(label7);
        this.add(label8);
        this.add(field1);
        this.add(field2);
        this.add(field3);
        this.add(field4);
        this.add(field5);
        this.add(field6);
        this.add(field7);
        this.add(field8);
        this.add(button1);
        this.add(button2);


        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Dorm dorm = new Dorm();
                dorm.setDormnum(field1.getText());
                dorm.setMember1(field2.getText());
                dorm.setMember2(field3.getText());
                dorm.setMember3(field4.getText());
                dorm.setMember4(field5.getText());
                dorm.setMember5(field6.getText());
                dorm.setMember6(field7.getText());
                dorm.setHead(field8.getText());

                DormService service = new DormServiceImpl();
                service.addDorm(dorm);

                JFrame frame = new JFrame();
                JOptionPane.showMessageDialog(frame,"添加成功!");
                field1.setText("");
                field2.setText("");
                field3.setText("");
                field4.setText("");
                field5.setText("");
                field6.setText("");
                field7.setText("");
                field8.setText("");
            }
        });

        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                new Index_admin(user).setVisible(true);
            }
        });
    }
}
